package com.shenhan.datasource.generator;

import com.alibaba.fastjson.JSON;
import com.shenhan.datasource.bean.MateriaBean;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.stream.IntStream;

/**
 * @author shenhan
 * @create 2020/9/10 12:03
 * @description 使用程序生成模拟疫情数据
 */
@Component
public class DataGenerator {
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Scheduled(initialDelay = 1000, fixedDelay = 1000 * 2)
    public void generate() {
        Random r = new Random();
        IntStream.range(0, 10).forEach(i -> {
            MateriaBean materiaBean = new MateriaBean(wzmc[r.nextInt(wzmc.length)],
                    wzly[r.nextInt(wzly.length)], r.nextInt(1000));
            //将数据发送到kafka
            kafkaTemplate.send("covid19_wz", r.nextInt(3), JSON.toJSONString(materiaBean));
        });
    }

    private String[] wzmc = new String[]{"N95口罩/个", "医用外科口罩/个", "84消毒液/瓶", "电子体温计/个",
            "一次性手套/副", "医用防护服/套"};
    private String[] wzly = new String[]{"采购", "下拨", "捐赠", "消耗", "需求"};
}
